/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.campusinfocompany.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.campusinfo.common.config.Global;
import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.common.utils.StringUtils;
import com.lczyfz.campusinfo.modules.campusinfocompany.entity.CampusinfoCompany;
import com.lczyfz.campusinfo.modules.campusinfocompany.service.CampusinfoCompanyService;

/**
 * 公司管理Controller
 * @author zms
 * @version 2018-04-17
 */
@Controller
@RequestMapping(value = "${adminPath}/campusinfocompany/campusinfoCompany")
public class CampusinfoCompanyController extends BaseController {

	@Autowired
	private CampusinfoCompanyService campusinfoCompanyService;
	
	@ModelAttribute
	public CampusinfoCompany get(@RequestParam(required=false) String id) {
		CampusinfoCompany entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = campusinfoCompanyService.get(id);
		}
		if (entity == null){
			entity = new CampusinfoCompany();
		}
		return entity;
	}
	
	@RequiresPermissions("campusinfocompany:campusinfoCompany:view")
	@RequestMapping(value = {"list", ""})
	public String list(CampusinfoCompany campusinfoCompany, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<CampusinfoCompany> page = campusinfoCompanyService.findPage(new Page<CampusinfoCompany>(request, response), campusinfoCompany); 
		model.addAttribute("page", page);
		return "modules/campusinfocompany/campusinfoCompanyList";
	}

	@RequiresPermissions("campusinfocompany:campusinfoCompany:view")
	@RequestMapping(value = "form")
	public String form(CampusinfoCompany campusinfoCompany, Model model) {
		model.addAttribute("campusinfoCompany", campusinfoCompany);
		return "modules/campusinfocompany/campusinfoCompanyForm";
	}

	@RequiresPermissions("campusinfocompany:campusinfoCompany:edit")
	@RequestMapping(value = "save")
	public String save(CampusinfoCompany campusinfoCompany, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, campusinfoCompany)){
			return form(campusinfoCompany, model);
		}
		// 根据请求对象中参数查询是否有数据
		CampusinfoCompany CompanyByname= campusinfoCompanyService.getByCompanyname(campusinfoCompany);// 判断数据是否相同
		if (CompanyByname==null){
			campusinfoCompanyService.save(campusinfoCompany);
			addMessage(redirectAttributes, "保存公司管理成功");
		} else if(!StringUtils.isBlank(campusinfoCompany.getId())){
			campusinfoCompanyService.save(campusinfoCompany);
			addMessage(redirectAttributes, "修改公司管理成功");
		} else{
			addMessage(redirectAttributes, "公司名称已经存在，请重新添加");
		}
		return "redirect:"+Global.getAdminPath()+"/campusinfocompany/campusinfoCompany/?repage";
	}
	
	@RequiresPermissions("campusinfocompany:campusinfoCompany:edit")
	@RequestMapping(value = "delete")
	public String delete(CampusinfoCompany campusinfoCompany, RedirectAttributes redirectAttributes) {
		campusinfoCompanyService.delete(campusinfoCompany);
		addMessage(redirectAttributes, "删除公司管理成功");
		return "redirect:"+Global.getAdminPath()+"/campusinfocompany/campusinfoCompany/?repage";
	}

}